from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List
from fastapi import APIRouter, Depends, HTTPException
from ..core.database import get_db
from ..core.dependencies import get_current_admin_user
from ..models.user import User
from ..models.employee import Employee as EmployeeModel
from ..schemas.employee import Employee, EmployeeCreate, EmployeeUpdate
from ..services.employee_service import EmployeeService

router = APIRouter(prefix="/employees", tags=["employees"])

@router.get("/", response_model=List[Employee])
def get_employees(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看员工列表
    employee_service = EmployeeService(db)
    return employee_service.get_employees(skip, limit)

@router.post("/", response_model=Employee)
def create_employee(employee: EmployeeCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以创建员工
    employee_service = EmployeeService(db)
    return employee_service.create_employee(employee)

@router.get("/{employee_id}", response_model=Employee)
def get_employee(employee_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看员工详情
    employee_service = EmployeeService(db)
    employee = employee_service.get_employee(employee_id)
    if not employee:
        raise HTTPException(status_code=404, detail="Employee not found")
    return employee

@router.put("/{employee_id}", response_model=Employee)
def update_employee(employee_id: int, employee: EmployeeUpdate, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以更新员工信息
    employee_service = EmployeeService(db)
    return employee_service.update_employee(employee_id, employee)

@router.delete("/{employee_id}")
def delete_employee(employee_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以删除员工
    employee_service = EmployeeService(db)
    employee_service.delete_employee(employee_id)
    return {"message": "Employee deleted successfully"}

@router.get("/department/{department_id}", response_model=List[Employee])
def get_employees_by_department(department_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看指定部门的员工列表
    employee_service = EmployeeService(db)
    return employee_service.get_employees_by_department(department_id)

@router.get("/{employee_id}/work-age")
def get_employee_work_age(employee_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看员工工龄
    employee_service = EmployeeService(db)
    return {"work_age_years": employee_service.get_employee_work_age(employee_id)}